package com.zjcode.cas.utils;

import com.zjcode.cas.config.RedisConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Redis的工具类
 *
 * @版权所有 上海至界信息科技有限公司
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisConfiguration redisConfiguration;
    @Autowired
    private JedisPool pool;


    /**
     * 更新
     *
     * @param key
     * @param value
     */
    public void update(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.auth(redisConfiguration.getPassword());
            jedis.psetex(key, redisConfiguration.getExpiredTime() * 1000L, value);
            // 如果key是对URL授权的角色编码，则不过期
            if (key.contains("TKMCodeCer:URL:ROLECODE")) {
                jedis.persist(key);
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 持久化数据
     */
    public void psetex(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.auth(redisConfiguration.getPassword());
            // 判断key是否存在
            if (!jedis.exists(key)) {
                jedis.psetex(key, redisConfiguration.getExpiredTime() * 1000L, value);
                // 如果key是对URL授权的角色编码，则不过期
                if (key.contains("TKMCodeCer:URL:ROLECODE")) {
                    jedis.persist(key);
                }
                System.out.println("写入成功");
            } else {
                System.out.println("key已存在,跳过设置");
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    /**
     * 持久化数据
     *
     * @param key
     * @param value
     * @param seconds
     */
    public void psetex(String key, String value, int seconds) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.auth(redisConfiguration.getPassword());
            // 判断key是否存在
            if (!jedis.exists(key)) {
                jedis.psetex(key, seconds * 1000L, value);
                // 如果key是对URL授权的角色编码，则不过期
                if (key.contains("TKMCodeCer:URL:ROLECODE")) {
                    jedis.persist(key);
                }
                System.out.println("写入成功");
            } else {
                System.out.println("key已存在,跳过设置");
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 根据键删除数据
     *
     * @param key
     */
    public void del(String key) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.auth(redisConfiguration.getPassword());
            jedis.del(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 根据键获取值
     *
     * @param key 键
     * @return
     */
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.auth(redisConfiguration.getPassword());
            return jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

}
